package Screens.MyScreens;

import java.lang.reflect.InvocationTargetException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.geom.Vector2f;

import GUI.GuiMainMenu;
import Game.GameHelper;
import Screens.Manager.Screen;
import Screens.Manager.ScreenManager;
import Tools.Configuration;
import Tools.ResourceManager;

/** Menu screen intro
 * @author Aikidos
 */
public class MenuScreen extends Screen {

	/** Game logo */
	private Image _logo = null;
	
	/** Game menu */
	private GuiMainMenu _guiMainMenu = null;
	
	/** Information font */
	private UnicodeFont _fontInfo = null;
	
	@Override
	public void init() throws SlickException, InstantiationException,
			IllegalAccessException, IllegalArgumentException,
			InvocationTargetException, NoSuchMethodException,
			SecurityException, ClassNotFoundException {
		
		// Initialize main menu
		_guiMainMenu = new GuiMainMenu();
		_guiMainMenu.addItem("START");
		_guiMainMenu.addItem("OPTIONS");
		_guiMainMenu.addItem("ABOUT");
		_guiMainMenu.addItem("EXIT");
		
	}

	@Override
	public void loadContent() throws SlickException {
		_logo = new Image("res/gfx/logo.png");
		
		// Load font
		_fontInfo = new UnicodeFont("res/fonts/mainfont.ttf", 10, false,
				false);
		_fontInfo.addAsciiGlyphs();
		_fontInfo.getEffects().add(new ColorEffect(java.awt.Color.LIGHT_GRAY));
		_fontInfo.loadGlyphs();
		
		try {
			// Load sounds
			if (!Configuration.isMute()) {
				ResourceManager.getPool().put("sfx_menu", new Sound("res/sfx/menu.wav"));
			}
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(int delta) throws SlickException,
			InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException, ClassNotFoundException {
		
		// Update menu
		_guiMainMenu.update(delta);
		
		// Control
		Input input = GameHelper.getGame().getInput();
		if (input.isKeyPressed(Input.KEY_RETURN)) {
			switch (_guiMainMenu.getSelected()) {
			case 0:
				ScreenManager.go("GameScreen"); break;
				
			case 3:
				System.exit(0); break;
			}
		}
	}

	@Override
	public void draw(Graphics g) throws SlickException {
		_logo.draw((GameHelper.getGame().getWidth() / 2) - (_logo.getWidth() / 2),
				((GameHelper.getGame().getHeight() / 2) - (_logo.getHeight() / 2)) - 40);
		
		// Draw menu
		_guiMainMenu.draw(new Vector2f(
				(GameHelper.getGame().getWidth() / 2) + 45,
				(GameHelper.getGame().getHeight() / 2) + 15), g);
		
		// Draw copyright
		String copyrightText = "(C) 2012 - " + String.valueOf(getCurrentYear() + 1) + " by Aikidos"; 
		_fontInfo.drawString(
				GameHelper.getGame().getWidth() - _fontInfo.getWidth(copyrightText) - 15, 
				GameHelper.getGame().getHeight() - _fontInfo.getHeight(copyrightText) - 15, copyrightText);
		
		// Draw version
		String versionText = "Dev. Build (v0.04)";
		_fontInfo.drawString(
				GameHelper.getGame().getWidth() - _fontInfo.getWidth(versionText) - 15, 
				15, versionText);
	}

	@Override
	public void unloadContent() throws SlickException {
		_logo.destroy();
		_fontInfo.destroy();
		_logo = null;
		_fontInfo = null;
		
		if (!Configuration.isMute()) {
			ResourceManager.getPool().remove("sfx_menu");
		}
	}

	@Override
	public void shutdown() {
		GameHelper.getGame().getGraphics().setBackground(Color.black);
		
	}
	
	
	private int getCurrentYear() {
		Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
		calendar.setTime(new Date());
		return calendar.get(Calendar.YEAR);
	}

}
